package paketFK;

import org.json.JSONException;
import org.json.JSONObject;

import paketDB.DatabaseHandler;
import paketDB.UserFunctions;
import paketGUI.MainMenuScreen;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.TextView;
import android.widget.Toast;
import eu.mvmnt.golfingapp.R;

@SuppressWarnings("rawtypes")
public class ProcessLogin extends AsyncTask {
	Activity activity;
	/**
	 * @uml.property name="databaseHandler"
	 * @uml.associationEnd multiplicity="(1 1)"
	 *                     inverse="processLogin:paketDB.DatabaseHandler"
	 * @uml.association name="baut Verbindung auf zu"
	 */
	private DatabaseHandler databaseHandler = null;
	String login;
	String password;
	private ProgressDialog pDialog;
	TextView txtLogin;

	TextView txtPassword;

	/**
	 * @uml.property name="userFunctions"
	 * @uml.associationEnd multiplicity="(1 1)"
	 *                     inverse="processLogin:paketDB.UserFunctions"
	 * @uml.association name="erzeugt"
	 */
	private UserFunctions userFunctions = null;

	@Override
	protected JSONObject doInBackground(Object... params) {
		userFunctions = new UserFunctions();
		JSONObject json = userFunctions.loginUser(login, password);
		return json;
	}

	@Override
	protected void onPostExecute(Object result) {
		JSONObject json = (JSONObject) result;
		try {
			if (json.getString("success") != null) { //$NON-NLS-1$
				String res = json.getString("success"); //$NON-NLS-1$
				if (Integer.parseInt(res) == 1) {
					pDialog.setMessage("Loading User Space"); //$NON-NLS-1$
					pDialog.setTitle("Getting Data"); //$NON-NLS-1$
					databaseHandler = new DatabaseHandler(
							activity.getApplicationContext());
					JSONObject json_user = json.getJSONObject("user"); //$NON-NLS-1$
					/**
					 * Clear all previous data in SQlite database.
					 **/
					UserFunctions logout = new UserFunctions();
					logout.logoutUser(activity.getApplicationContext());
					databaseHandler
							.addUser(
									json_user.getString("Vorname"), json_user.getString("Nachname"), json_user.getString("Email"), json_user.getString("CreatedAt")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
					/**
					 * If JSON array details are stored in SQlite it launches
					 * the User Panel.
					 **/
					Intent upanel = new Intent(
							activity.getApplicationContext(),
							MainMenuScreen.class);
					upanel.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					pDialog.dismiss();
					activity.startActivity(upanel);
					/**
					 * Close Login Screen
					 **/
					activity.finish();
				} else {
					pDialog.dismiss();
					Toast.makeText(activity.getApplicationContext(),
							"Incorrect password/username", Toast.LENGTH_SHORT).show(); //$NON-NLS-1$
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		txtPassword = (TextView) activity.findViewById(R.id.txtPassword);
		txtLogin = (TextView) activity.findViewById(R.id.txtLogin);

		login = txtLogin.getText().toString();
		password = txtPassword.getText().toString();

		pDialog = new ProgressDialog(activity);
		pDialog.setTitle("Contacting Servers"); //$NON-NLS-1$
		pDialog.setMessage("Logging in..."); //$NON-NLS-1$
		pDialog.setIndeterminate(false);
		pDialog.setCancelable(true);
		pDialog.show();
	}

	public void setActivity(Activity activity) {
		this.activity = activity;
	}

}
